package org.nobject.common.swing;

import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.RepaintManager;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;

import org.nobject.common.lang.StringUtils;
import org.nobject.common.swing.g.GConstance;

/**
 * ComponentUtils
 *
 * @author Benson
 *
 */
public class ComponentUtils {
	
	/**
	 * getComponents
	 * @param c
	 * @param l
	 * @return
	 */
	public static List<Component> getAllComponents(Container c,List l){
		if(l==null){
			l=new LinkedList();
			l.add(c);
		} 
		
		Component[] cs=c.getComponents();
		if(cs!=null&&cs.length>0){
			for (int i = 0; i < cs.length; i++) {
				l.add(cs[i]);
				Component [] ccs=((Container)cs[i]).getComponents();
				if(ccs==null||ccs.length==0) continue;
				l.addAll(getAllComponents((Container)cs[i],l));
			}
		}
		return l;
	}
	
	//设置全局字体
	public static void initGlobalFontSetting(Font fnt){
		GConstance.FONTSMOOTH=true;
		RepaintManager.setCurrentManager(new FullRepaintManager());
	    FontUIResource fontRes = new FontUIResource(fnt);
	    for(Enumeration keys = UIManager.getDefaults().keys(); keys.hasMoreElements();){
	        Object key = keys.nextElement();
	        Object value = UIManager.get(key);
	        if(value instanceof FontUIResource) UIManager.put(key, fontRes);
	    }
	}
	
	public static void hideElements(){
		
	}
	
	/**
	 * 获取指定类型组件
	 * @param container
	 * @param order
	 * @param cmpClazz
	 * @return
	 */
	public static Component getComponent(Container container,int order,Class cmpClazz){
		int c=container.getComponentCount();
		int ic=-1;
		for (int i = 0; i < c; i++) {
			Component comp=container.getComponent(i);
			if(cmpClazz.equals(comp.getClass())){
				ic++;
				if(ic==order)return comp;
			}
		}
		return null;
	}
	
	/**
	 * 获取组件
	 * @param container 容器
	 * @param cmpClazz 组件类型
	 * @return
	 */
	public static List getComponents(Container container,Class cmpClazz){
		return getComponents(container, cmpClazz,null);
	}
	
	/**
	 * 获取组件
	 * @param container 容器
	 * @param cmpClazz 组件类型
	 * @param result 结果
	 * @return
	 */
	private static List getComponents(Container container,Class cmpClazz,List result){
		if(result==null) result=new LinkedList();
		Component [] cmps=container.getComponents();
		for (int i = 0; i < cmps.length; i++) {
			if(cmps[i].getClass().isAssignableFrom(Container.class)){
				getComponents((Container)cmps[i],cmpClazz,result);
			}else if(cmpClazz==null||cmps[i].getClass().equals(cmpClazz)){
				result.add(cmps[i]);
			}
		}
		return result;
	}
	
	/**
	 * 清理组件
	 * @param container 容器
	 */
	public static void clearComponents(Container container){
		Component[] cmps= container.getComponents();
		clearComponents(cmps);
	}
	
	/**
	 * 清理组件
	 * @param components 组件
	 */
	public static void clearComponents(Component... components){
		if(components!=null){
			for (int i = 0; i < components.length; i++) {
				if(components[i].getClass().equals(JCheckBox.class)){
					((JCheckBox)components[i]).setSelected(false);
				}else if(components[i].getClass().equals(JTextField.class)){
					((JTextField)components[i]).setText("");
				}else if(components[i].getClass().equals(JComboBox.class)){
					((JComboBox)components[i]).setSelectedIndex(0);
				}else{
					clearComponents((Container)components[i]);
				}
			}
		}
	}
	
	/**
	 * supercontainer
	 */
	public static Container supercontainer;
	
	/**
	 * getComponentByName
	 * @param name
	 * @return
	 */
	public static Component getComponentByName(String name){
		return getComponentByName(supercontainer,name);
	}
	
	/**
	 * getComponentByName
	 * @param supercontainer
	 * @param name
	 * @return
	 */
	public static Component getComponentByName(Container supercontainer,String name){
		Component[] cmps=supercontainer.getComponents();
		for (int i = 0; i < cmps.length; i++) {
			String cmpName=cmps[i].getName();
			if(!StringUtils.isEmpty(cmpName)&&cmpName.equals(name)){
				return cmps[i];
			}else if(Container.class.isAssignableFrom(cmps[i].getClass())){
				Component rc=getComponentByName((Container)cmps[i],name);
				if(rc!=null) return rc;
			}
		}
		return null;
	}
	
	/**
	 * moveToCenter
	 */
	public static void moveToCenter(Component p){
		if(p!=null){
			Container c=p.getParent();
			System.out.println(c.getWidth()+":"+c.getHeight()+":"+p.getWidth()+":"+p.getHeight());
			
			
		}
	}
	
}
